/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jheuristics.ga.operators.reproductors;

import org.epanetgrid.otimizacao.log.AGLogger;
import org.jheuristics.Individual;
import org.jheuristics.ga.DefaultPopulation;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Population;
import org.jheuristics.ga.exception.IllegalIndividualException;
import org.jheuristics.ga.operators.Probability;
import org.jheuristics.ga.operators.Reproductor;


/**
 * TODO
 *
 * @author Marcell Manfrin, marcell@ourgrid.org, Jan 9, 2006
 */
public abstract class AbstractMutation extends AbstractReproductorDecorator {

	protected AGLogger logger = AGLogger.getInstance();
	
	/**
	 * TODO
	 *
	 * @param probability Probabilidade de mutacao
	 * @param tryOuts Quantidade m�xima de tentativas
	 */
	public AbstractMutation(Probability probability, int tryOuts) {
		super(probability, tryOuts);
	}

    /**
     * TODO
     *
	 * @param decoretedReproductor reprodutor
	 * @param probability Probabilidade de mutacao
	 * @param tryOuts Quantidade m�xima de tentativas
     */
    public AbstractMutation(Reproductor decoretedReproductor, Probability probability, int tryOuts) {
		super(decoretedReproductor, probability, tryOuts);
	}

	/**
     * TODO
     *
     * @return
     * @see org.jheuristics.ga.operators.Reproductor#getRequiredNumberOfParents()
     */
    public int getRequiredNumberOfParents() {
        return 0;
    }

    /**
     * TODO
     *
     * @param parents
     * @param status
     * @param config
     * @return
     * @see org.jheuristics.ga.operators.Reproductor#reproduce(org.jheuristics.Individual[], org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
     */
    public Individual[] reproduce(Individual[] parents, GAStatus status, GAConfig config) {
    	if (null == parents) {
			// TODO: ERROR MSG
            throw new NullPointerException();
    	}
    	for (int i = 0; i < parents.length; i++) {
    		if (null == parents[i]) {
    			// TODO: ERROR MSG
                throw new NullPointerException();
    		}
    	}
        if (null == config) {
			// TODO: ERROR MSG
            throw new NullPointerException();
        }
        Population result = new DefaultPopulation();
        for (int i = 0; i < parents.length; i++) {
			Individual mutaded = null;
	        for (int j = 0; mutaded == null && j <= getMaxBadReproductionAttempts(); j++) {
	            try {
					mutaded = mutate(parents[i], status, config);
	            } catch (IllegalIndividualException e) {
    	            // if gens aren accept try again
	            }
	        }
			if (null != mutaded) {
				result.addIndividual(mutaded);
			} else {
				result.addIndividual(parents[i]);
			}
        }
//        StringBuffer buffer = new StringBuffer("\nResultado da reprodu��o: \n");
//        Individual[] ind = result.toIndividualArray();
//        for (int i = 0; i < ind.length; i++) {
//			buffer.append(ind[i]);
//			buffer.append("\n");
//		}
//        SPLogger.getInstancia().log(SPLogger.OTIMIZACAO, SPLogger.DEBUG, buffer);
        return result.toIndividualArray();
    }

	/**
	 * TODO
	 *
	 * @param individual
	 * @param status
	 * @param config
	 * @return
	 */
	protected abstract Individual mutate(Individual individual, GAStatus status, GAConfig config);

}
